//import axios from 'axios';

//const API_URL = 'https://jsonplaceholder.typicode.com/users';

/**
export const UserService = {
  async getAllUsers() {
    try {
      const response = await axios.get(API_URL);
      return response.data;
    } catch (error) {
      console.error('Error fetching users:', error);
      throw error;
    }
  },

  async getUserById(id) {
    try {
      const response = await axios.get(`${API_URL}/${id}`);
      return response.data;
    } catch (error) {
      console.error(`Error fetching user ${id}:`, error);
      throw error;
    }
  },

  async createUser(userData) {
    try {
      const response = await axios.post(API_URL, userData);
      return response.data;
    } catch (error) {
      console.error('Error creating user:', error);
      throw error;
    }
  },

  async updateUser(id, userData) {
    try {
      const response = await axios.put(`${API_URL}/${id}`, userData);
      return response.data;
    } catch (error) {
      console.error(`Error updating user ${id}:`, error);
      throw error;
    }
  },

  async deleteUser(id) {
    try {
      await axios.delete(`${API_URL}/${id}`);
      return id;
    } catch (error) {
      console.error(`Error deleting user ${id}:`, error);
      throw error;
    }
  }
};  */
import axios from 'axios';
//import { url } from 'inspector';

const API_BASE_URL = 'http://localhost:8080/api/';

export const UserService = {
  async getAllUsers() {
    try {
      const response = await axios.get(API_BASE_URL+'users');

      return response.data;
    } catch (error) {
      console.error('Error fetching users:', error);
      throw error;
    }
  },

  async getUserById(id) {
    try {
      const response = await axios.get(`${API_BASE_URL}users/${id}`);
      return response.data;
    } catch (error) {
      console.error(`Error fetching user ${id}:`, error);
      throw error;
    }
  },

  async createUser(userData) {
    try {
      const url = API_BASE_URL + 'user';
      console.log("-----------"+url);
      const response = await axios.post(url, userData);
      return response.data;
    } catch (error) {
      console.error('Error creating user:', error);
      throw error;
    }
  },

  async updateUser(id, userData) {
    try {
      console.log(userData)
      userData.id=id;
      const response = await axios.put(`${API_BASE_URL}users`, userData);
      return response.data;
    } catch (error) {
      console.error(`Error updating user ${id}:`, error);
      throw error;
    }
  },

  async deleteUser(id) {
    try {
      const response = await axios.delete(`${API_BASE_URL}users/${id}`);
      return response.data;
    } catch (error) {
      console.error(`Error deleting user ${id}:`, error);
      throw error;
    }
  }
};

// Create an Axios instance with base configuration
const apiClient = axios.create({
  baseURL: 'http://localhost:8080/api',
  headers: {
    'Content-Type': 'application/json'
    // You can add authorization headers here if needed
    // 'Authorization': `Bearer ${token}`
  }
});

export default apiClient;